package com.bci.pwtz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.exception.PwtzException;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.service.CompanyUserService;

@Controller
@RequestMapping("/tempuser")
public class TempUserController extends AbstractController{
	 @Autowired
	  private CompanyUserService     companyUserService;
	
	@RequestMapping(value="/templist",method=RequestMethod.POST)
    @ResponseBody
    public PaginationList<CompanyUser> templist(int pageNo,int pageSize){
    	PaginationList<CompanyUser> list= new PaginationList<CompanyUser>();
    	try {
			list = companyUserService.selectTmepUser(pageNo, pageSize);
		} catch (PwtzDatabaseException e) {
			e.printStackTrace();
		}
		return list;
    }
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> login(HttpServletRequest req,HttpServletResponse resp,Long Id)
    {
        Map<String, String> respMap = new HashMap<String, String>(2);
        try
        {
                CompanyUser user = companyUserService.load(Id);
                if (null != user && companyUserService.selectTmepUserById(user.getCompanyUserId()) >0)
                {
                    Long userId = user.getCompanyUserId();
                    session.setAttribute("user", user);
                    session.setAttribute("userId", userId);
                    List<String> actionList=getCurrentAction();
                    session.setAttribute("actionList", actionList);
                    respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null,
                            "登录成功");
                }
                else
                {
                    respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,
                            "VisitInvalidParametersException", "该体验用户不存在或已失效！");
                }
            
           
        }
        catch (PwtzException e)
        {
            respMap = packageUppException(e);
        }
        catch (Exception e)
        {
            respMap = packageException(e);
        }

        return respMap;
    }
	
}
